package com.sesame;

import com.github.kevinsawicki.http.HttpRequest;
import com.sesame.config.ProjectConfig;
import kim.sesame.framework.utils.ZipCompressor;
import kim.sesame.framework.web.context.SpringContextUtil;
import kim.sesame.framework.web.init.SystemStart;
import kim.sesame.framework.web.util.FileUtil;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author johnny
 * @date 2017-06-10 12:41
 * @classpath com.sesame
 * @Description:@ServletComponentScan : 监听器
 */
@ServletComponentScan(basePackageClasses = SystemStart.class)
@SpringBootApplication
public class AppCode {


    static Logger log = Logger.getLogger(AppCode.class);

    public static void main(String[] arr) throws IOException {

        ConfigurableApplicationContext context = SpringApplication.run(AppCode.class, arr);

        SpringContextUtil.printInfo();

        ProjectConfig config = SpringContextUtil.getApplicationContext().getBean(ProjectConfig.class);
        if (!config.isLocalThemePath()) {
            init();
        }

    }

    public static void init() {
         FileUtil.cleanDir(new File(SpringContextUtil.getCurrentPath()));
        String zipPath = SpringContextUtil.getCurrentPath() + "/theme.zip";
        // 1 复制zip 到指定的目录
        InputStream in = AppCode.class.getResourceAsStream("/com/sesame/theme.zip");
        File file = new File(zipPath);
        try {
            FileUtils.copyInputStreamToFile(in, file);
            in.close();

            // 2 解压文件到当前目录
            ZipCompressor.unZipFiles(file, SpringContextUtil.getCurrentPath() + "/");
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.gc();
        //3 删除zip
        FileUtil.clearFiles(new File(zipPath));
    }


}